package jeecg.business.thread.task.impl;

import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.log.CollLogEntity;
import jeecg.business.entity.site.SimpleSiteEntity;
import jeecg.business.entity.site.SiteEntity;
import jeecg.business.thread.HtmlFetchManage;
import jeecg.business.vo.ProxyIpVo;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.util.DataUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.HtmlCleanBaseUtil;
import org.jeecgframework.core.util.clean.ListHtmlCleanUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.http.HttpClientUtil;
/**
 * 
 * <p>
 * 标题: 搜索-需要使用代理
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since 2015-6-7 上午9:15:26
 */
public class SearchProxyExecTask extends CommonExecTask{
	
	String ip;
	
	int port;

	private static final String String = null;

	public SearchProxyExecTask(EnterEntity enter , KeywordEntity keyword, SiteEntity site) {
		super(enter , keyword, site);
	}
	
	void getIpAndPort(){
		ProxyIpVo ips;
		try {
			ips = HtmlFetchManage.getIpCanUsed();
			String []ipport = ips.getIpandport().split(":");
			ip = ipport[0];
			port = Integer.parseInt(ipport[1]);
		} catch (Exception e) {
			
		}
	}
	
	public String getHtmlCanUseIp(String href){
		String baseurl = href;
		String html = null;
		if(HtmlFetchManage.proxyIsUse == 1){
			do{
				getIpAndPort();
				if((port+"").startsWith("80") || (port+"").startsWith("90")){
					html = HttpClientUtil.getSourceFromUrl(baseurl, site.getWyzfj(), null , ip , port);
				}
				System.out.println("切换一个ip，打印避免死循环");
			}while(StringUtil.isEmpty(html) || html.contains(site.getBftzm()));
			System.out.println("可用IP："+ip+"-"+port);
		}else{
			html = HttpClientUtil.getSourceFromUrl(baseurl, site.getWyzfj(), null);
		}
		return html;
	}

	@Override
	public void run() {
		CollLogEntity log = null;
		log = saveLog();
		try{
			System.out.println("exec task......");
			String basehref = site.getZddz();
			String word = URLEncoder.encode(keyword.getKeyword(),site.getWyzfj());
			basehref = basehref.replace(KEYWORD_FLAG, word);
			//在设置时间，近一个月 {starttime} {endtime}
			basehref = basehref.replace(LONG_STARTTIME_FLAG, getLongStartTime());
			basehref = basehref.replace(STARTTIME_FLAG, getStartTime());
			basehref = basehref.replace(ENDTIME_FLAG, getEndTime());
			
			do{
				current++;
				System.out.println(keyword.getKeyword()+"-----"+site.getMc()+"-----第"+current+"页"+"-----"+basehref);
				if(StringUtil.isNotEmpty(basehref)){
					String lbhtml = null;
					if(ip == null)		//第一次
						lbhtml = getHtmlCanUseIp(basehref);
					else{
						lbhtml = HttpClientUtil.getSourceFromUrl(basehref, site.getWyzfj(), null , ip , port);
						while(StringUtil.isEmpty(lbhtml) || lbhtml.contains(site.getBftzm())){
							lbhtml = getHtmlCanUseIp(basehref);
						}
					}
					
					List<HtmlParseResult> infolist = ListHtmlCleanUtil.listInfo4Html(lbhtml, site, keyword.getKeyword() , basehref);
					String contenthref = null;
					if(infolist != null && infolist.size()>0){
						KeyinfoEntity keyinfo = null;
						for(HtmlParseResult result : infolist){
							contenthref = result.getUrl();
							contenthref = contenthref.replace("&amp;", "&");
							if(!contenthref.toLowerCase().startsWith("http"))
								contenthref = site.getGdz()+contenthref;
							
							System.out.println("跳转前链接："+contenthref);
							
							/**
							 * 判断是否有二次跳转
							 */
							if("1".equals(site.getRctz()) && StringUtil.isNotEmpty(site.getRcljqb()) && contenthref.startsWith(site.getGdz())){
								String html = HttpClientUtil.getSourceFromUrl(contenthref, site.getWyzfj(), cookies , ip , port);
								int p = html.indexOf(site.getRcljqb());
								if(p>=0){
									contenthref = html.substring(p+site.getRcljqb().length(),html.indexOf(site.getRcljhb(), p+site.getRcljqb().length()));
								}
							}
							
							System.out.println("抓取-------"+contenthref);
//							if(contenthref.contains("news.fznews.com.cn"))
//								System.out.println("mc");
							
							if(keyinfoService.hasHref(contenthref , enter.getId())) continue;
							try{
								/**
								 * 设置内容
								 */
								String roothref = StringUtil.getRootHref(contenthref);
								SimpleSiteEntity simpleSite = simpleSiteService.query(roothref);
								
								String bmlx = "utf-8";
								
								if(simpleSite == null){		//添加到数据库中。
									simpleSite = new SimpleSiteEntity();
									simpleSite.setZdbs(roothref);
									simpleSite.setSfpp("2");
									simpleSite.setYldz(contenthref);
									if(contenthref.startsWith(site.getGdz()))
										simpleSite.setZnbm("1");
									else
										simpleSite.setZnbm("2");
									simpleSite.setWzlx("ssyq");
									bmlx = getEncodeByHref(contenthref, bmlx, cookies);
									simpleSite.setBmlx(bmlx);
									simpleSite.setZwnr("//body");
									simpleSiteService.save(simpleSite);
								}else{
									bmlx = simpleSite.getBmlx();
									if("1".equals(simpleSite.getZnbm()))
										bmlx = getEncodeByHref(contenthref, bmlx, cookies);
									if(StringUtil.isEmpty(bmlx)){
										bmlx = "utf-8";
										bmlx = getEncodeByHref(contenthref, bmlx, cookies);
										simpleSite.setBmlx(bmlx);
										simpleSiteService.updateEntitie(simpleSite);
									}
								}
								
								/**
								 * 获取内容
								 */
								TagNode node = null;
								HtmlCleaner cleaner = new HtmlCleaner();
								String html = HttpClientUtil.getSourceFromUrl(contenthref, bmlx, cookies);
								node = cleaner.clean(html);
								String zwnrXpath = "//body";
								if(StringUtil.isNotEmpty(simpleSite.getZwnr())) zwnrXpath = simpleSite.getZwnr();
								String content = HtmlCleanBaseUtil.getTagContent(node, zwnrXpath);
								
								keyinfo = new KeyinfoEntity(result);
								if(StringUtil.isNotEmpty(content)){
									content = StringUtil.removeHTMLLableExe(content);
									keyinfo.setBodycontent(content);
								}
								
								if("1".equals(site.getSjhqfs())){	//获取时间
									/**
									 * 时间
									 */
									String time = null;
									String timeXpath = null;
									String timeReg = null;
									if(StringUtil.isNotEmpty(site.getZwsj())){
										if(site.getZwsj().indexOf("@|@")>0){
											String [] array = site.getZwsj().split("@\\|@");
											timeXpath = array[0];
											timeReg = array[1];
											time = HtmlCleanBaseUtil.getTagContent(node, timeXpath);
											Pattern p = Pattern.compile(timeReg);
											Matcher m = p.matcher(time);
											while(m.find()){
												time = m.group();
												if(StringUtil.isNotEmpty(time) && time.startsWith("20"))
													break;
											}
										}
										if(StringUtil.isNotEmpty(time)){
											Date eventdate = DataUtils.parseDate(time);
											result.setEventDate(eventdate);
										}
									}
								}
									
								keyinfo.setQyid(enter.getId());
								System.out.println(result.getUrl()+"----"+result.getTitle()+"----"+(keyinfo.getBodycontent()!=null?keyinfo.getBodycontent().length():0)+"----"+result.getEventDate());
								if(filter(enter, keyinfo , keyword))		//过滤
									keyinfoService.saveKeyinfo(keyinfo);
							}catch(Exception e){
//								e.printStackTrace();
								System.out.println("抓取失败："+contenthref);
							}
						}
						basehref = infolist.get(0).getNextPageUrl();
						basehref = basehref.replace("&amp;", "&");
//						System.out.println("下一个地址："+basehref);
						
						if(basehref!=null && !basehref.startsWith("http"))
							basehref = site.getGdz()+basehref;
					}
				}
			}while(current <= maxpage);
//			log = collLogService.get(CollLogEntity.class, log.getId());
		}catch(Exception e){
//			e.printStackTrace();
//			log.setSfcg("2");
//			log.setCwxx(e.getMessage());
		}finally{
			log = collLogService.get(CollLogEntity.class, log.getId());
			log.setJssj(new Date());
			collLogService.updateEntitie(log);
			System.out.println("end....");
		}			
	}

}
